package com.loong.musicdemo.ui.mine.recent.fragment;

import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.loong.musicdemo.R;
import com.loong.musicdemo.model.Song;
import com.loong.musicdemo.ui.mine.adapter.SongAdapter;
import com.loong.musicdemo.util.dataDeal.MusicDeal;
import com.loong.musicdemo.util.dataDeal.MvDeal;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import static android.content.Context.MODE_PRIVATE;


public class RecentSongFragment extends Fragment {
    private RecyclerView recent_songs_view;
    private ProgressBar progressBar;
//    private SmartRefreshLayout loadingMore;
    private List<String> ids = new ArrayList<>();
    private List<Song> recordSongs = new ArrayList<>();
    private List<String> mvIds = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_recent_song, container, false);
        recent_songs_view  = root.findViewById(R.id.recent_songs_view);
        progressBar = root.findViewById(R.id.progress_bar);
//        loadingMore = root.findViewById(R.id.loading_more);
//        initLoadingMoreSong();
        new RecordTask().execute();
        return root;
    }

//    /**
//     * 初始化刷新控件的事件
//     */
//    private void initLoadingMoreSong(){
//        loadingMore.setEnableRefresh(false);
//        loadingMore.setOnLoadmoreListener(new OnLoadmoreListener() {
//            @Override
//            public void onLoadmore(RefreshLayout refreshlayout) {
//
//            }
//        });
//    }

    /**
     * 异步任务请求与用户有关的最近播放音乐的id
     * 获取最近播放的音乐
     * 获取最近播放中的mvIds
     */
    class RecordTask extends AsyncTask<String,Void,List<Song>>{

        @Override
        protected List<Song> doInBackground(String... strings) {
            SharedPreferences userInfo = getContext().getSharedPreferences("userInfo",MODE_PRIVATE);
            String userId = userInfo.getString("userId", null);
            //获取用户最近播放音乐的id
            ids = MusicDeal.getWeekPlayRecordIds(userId);
            recordSongs = MusicDeal.getSongListByIds(ids);
            mvIds = MvDeal.getMvSongsIdFromSongList(recordSongs);
            return recordSongs;
        }

        @Override
        protected void onPostExecute(List<Song> songs) {
            super.onPostExecute(songs);
            //将用户最近播放音乐含有mv的存入到SharedPreferences存储中
            SharedPreferences recentId = getActivity().getSharedPreferences("mvIds",MODE_PRIVATE);
            SharedPreferences.Editor editor = recentId.edit();
            editor.putInt("size",mvIds.size());
            if(mvIds.size()!=recentId.getInt("size",0)&&mvIds.size()!=0){
                editor.clear().apply();
                Log.i("insdsa",mvIds.size()+"");
                for(int i = 0;i<mvIds.size();i++){
                    editor.putString("mvId_"+i,mvIds.get(i));
                }
                editor.apply();
            }
            LinearLayoutManager manager = new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false);
            SongAdapter adapter = new SongAdapter(songs);
            recent_songs_view.setLayoutManager(manager);
            recent_songs_view.setAdapter(adapter);
            progressBar.setVisibility(View.GONE);
        }
    }


}
